
define(['jquery','jq_page'], () => {
    $.get('../data/goods.json').then((res) => {
        class Mr_miao {
            constructor() {
                this.dataList = [];
                this.dataList = res.slice(0, 400);
                this.ul = document.querySelector('#main .goods ul');
                this.species = document.querySelector('#main .species ul');
                this.btn = document.querySelector('.search_son');
                this.input = document.querySelector('.input_son');
                this.ul_p = document.querySelector('.brand ul');
                this.init();
            }
            init() {
                this.getNum();
                this.render();
                this.sentiment();
                this.price();
                this.clicktoDetails();
                this.category();
                this.search();
                this.the_brand();
            }
            // 获得数据
            getNum() {
                // function theRandomNumber(a, b) {
                //     var min = Math.min(a, b)
                //     var max = Math.max(a, b)
                //     return Math.round(Math.random() * (max - min) + min)
                // }
                // function getarr() {
                //     for (let i = 1; i <= 400; i++) {
                //         arr.push(theRandomNumber(100, 200) * 1000)
                //     }
                //     return arr
                // }
                // console.log(getarr())
                var goods_img_arr = ['https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F01%2F210924151G35218-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1663870364&t=76cfdf1d6b9c19e4ef7777c19e5dd5b7','https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg1.doubanio.com%2Fview%2Fnote%2Fl%2Fpublic%2Fp81475089.jpg&refer=http%3A%2F%2Fimg1.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1663870364&t=1cdb8d3f93989cb5a891f60ac7bf834c','https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F02%2F2109251P4155T6-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1663870364&t=20d2181e5b8fc1fc8f4a38a54230e832']
                var arr4 = [10000, 69000, 19000, 11000, 62000, 98000, 26000, 51000, 70000, 96000, 74000, 50000, 54000, 63000, 19000, 82000, 5000, 22000, 66000, 8000, 71000, 5000, 77000, 63000, 52000, 37000, 24000, 45000, 72000, 51000, 90000, 31000, 28000, 16000, 83000, 86000, 56000, 60000, 36000, 66000, 73000, 96000, 66000, 68000, 66000, 23000, 64000, 21000, 42000, 81000, 84000, 66000, 88000, 2000, 17000, 67000, 95000, 75000, 86000, 8000, 7000, 56000, 44000, 24000, 69000, 71000, 37000, 90000, 87000, 9000, 93000, 44000, 73000, 96000, 70000, 40000, 59000, 12000, 66000, 79000, 98000, 4000, 89000, 9000, 4000, 86000, 86000, 10000, 80000, 88000, 6000, 54000, 83000, 80000, 5000, 53000, 60000, 25000, 90000, 22000]
                var arr1 = [10000, 69000, 19000, 11000, 62000, 98000, 26000, 51000, 70000, 96000, 74000, 50000, 54000, 63000, 19000, 82000, 5000, 22000, 66000, 8000, 71000, 5000, 77000, 63000, 52000, 37000, 24000, 45000, 72000, 51000, 90000, 31000, 28000, 16000, 83000, 86000, 56000, 60000, 36000, 66000, 73000, 96000, 66000, 68000, 66000, 23000, 64000, 21000, 42000, 81000, 84000, 66000, 88000, 2000, 17000, 67000, 95000, 75000, 86000, 8000, 7000, 56000, 44000, 24000, 69000, 71000, 37000, 90000, 87000, 9000, 93000, 44000, 73000, 96000, 70000, 40000, 59000, 12000, 66000, 79000, 98000, 4000, 89000, 9000, 4000, 86000, 86000, 10000, 80000, 88000, 6000, 54000, 83000, 80000, 5000, 53000, 60000, 25000, 90000, 22000]
                var arr2 = [10000, 69000, 19000, 11000, 62000, 98000, 26000, 51000, 70000, 96000, 74000, 50000, 54000, 63000, 19000, 82000, 5000, 22000, 66000, 8000, 71000, 5000, 77000, 63000, 52000, 37000, 24000, 45000, 72000, 51000, 90000, 31000, 28000, 16000, 83000, 86000, 56000, 60000, 36000, 66000, 73000, 96000, 66000, 68000, 66000, 23000, 64000, 21000, 42000, 81000, 84000, 66000, 88000, 2000, 17000, 67000, 95000, 75000, 86000, 8000, 7000, 56000, 44000, 24000, 69000, 71000, 37000, 90000, 87000, 9000, 93000, 44000, 73000, 96000, 70000, 40000, 59000, 12000, 66000, 79000, 98000, 4000, 89000, 9000, 4000, 86000, 86000, 10000, 80000, 88000, 6000, 54000, 83000, 80000, 5000, 53000, 60000, 25000, 90000, 22000]
                var arr3 = [10000, 69000, 19000, 11000, 62000, 98000, 26000, 51000, 70000, 96000, 74000, 50000, 54000, 63000, 19000, 82000, 5000, 22000, 66000, 8000, 71000, 5000, 77000, 63000, 52000, 37000, 24000, 45000, 72000, 51000, 90000, 31000, 28000, 16000, 83000, 86000, 56000, 60000, 36000, 66000, 73000, 96000, 66000, 68000, 66000, 23000, 64000, 21000, 42000, 81000, 84000, 66000, 88000, 2000, 17000, 67000, 95000, 75000, 86000, 8000, 7000, 56000, 44000, 24000, 69000, 71000, 37000, 90000, 87000, 9000, 93000, 44000, 73000, 96000, 70000, 40000, 59000, 12000, 66000, 79000, 98000, 4000, 89000, 9000, 4000, 86000, 86000, 10000, 80000, 88000, 6000, 54000, 83000, 80000, 5000, 53000, 60000, 25000, 90000, 22000]
                var arr = arr1.concat(arr2,arr3,arr4);
                var newArr1 = [127000, 135000, 169000, 130000, 195000, 184000, 118000, 143000, 151000, 153000, 199000, 184000, 143000, 164000, 139000, 133000, 143000, 131000, 126000, 112000, 113000, 174000, 159000, 164000, 119000, 158000, 191000, 122000, 182000, 130000, 123000, 116000, 137000, 132000, 150000, 159000, 140000, 194000, 148000, 103000, 168000, 192000, 125000, 113000, 101000, 141000, 159000, 183000, 132000, 182000, 140000, 169000, 151000, 111000, 189000, 149000, 120000, 129000, 167000, 194000, 198000, 170000, 151000, 180000, 104000, 131000, 118000, 106000, 178000, 115000, 177000, 131000, 183000, 117000, 195000, 189000, 173000, 192000, 147000, 185000, 187000, 195000, 190000, 178000, 112000, 146000, 125000, 177000, 160000, 159000, 140000, 118000, 104000, 195000, 154000, 174000, 125000, 176000, 102000, 129000]
                var newArr2 = [127000, 135000, 169000, 130000, 195000, 184000, 118000, 143000, 151000, 153000, 199000, 184000, 143000, 164000, 139000, 133000, 143000, 131000, 126000, 112000, 113000, 174000, 159000, 164000, 119000, 158000, 191000, 122000, 182000, 130000, 123000, 116000, 137000, 132000, 150000, 159000, 140000, 194000, 148000, 103000, 168000, 192000, 125000, 113000, 101000, 141000, 159000, 183000, 132000, 182000, 140000, 169000, 151000, 111000, 189000, 149000, 120000, 129000, 167000, 194000, 198000, 170000, 151000, 180000, 104000, 131000, 118000, 106000, 178000, 115000, 177000, 131000, 183000, 117000, 195000, 189000, 173000, 192000, 147000, 185000, 187000, 195000, 190000, 178000, 112000, 146000, 125000, 177000, 160000, 159000, 140000, 118000, 104000, 195000, 154000, 174000, 125000, 176000, 102000, 129000]
                var newArr3 = [127000, 135000, 169000, 130000, 195000, 184000, 118000, 143000, 151000, 153000, 199000, 184000, 143000, 164000, 139000, 133000, 143000, 131000, 126000, 112000, 113000, 174000, 159000, 164000, 119000, 158000, 191000, 122000, 182000, 130000, 123000, 116000, 137000, 132000, 150000, 159000, 140000, 194000, 148000, 103000, 168000, 192000, 125000, 113000, 101000, 141000, 159000, 183000, 132000, 182000, 140000, 169000, 151000, 111000, 189000, 149000, 120000, 129000, 167000, 194000, 198000, 170000, 151000, 180000, 104000, 131000, 118000, 106000, 178000, 115000, 177000, 131000, 183000, 117000, 195000, 189000, 173000, 192000, 147000, 185000, 187000, 195000, 190000, 178000, 112000, 146000, 125000, 177000, 160000, 159000, 140000, 118000, 104000, 195000, 154000, 174000, 125000, 176000, 102000, 129000]
                var newArr4 = [127000, 135000, 169000, 130000, 195000, 184000, 118000, 143000, 151000, 153000, 199000, 184000, 143000, 164000, 139000, 133000, 143000, 131000, 126000, 112000, 113000, 174000, 159000, 164000, 119000, 158000, 191000, 122000, 182000, 130000, 123000, 116000, 137000, 132000, 150000, 159000, 140000, 194000, 148000, 103000, 168000, 192000, 125000, 113000, 101000, 141000, 159000, 183000, 132000, 182000, 140000, 169000, 151000, 111000, 189000, 149000, 120000, 129000, 167000, 194000, 198000, 170000, 151000, 180000, 104000, 131000, 118000, 106000, 178000, 115000, 177000, 131000, 183000, 117000, 195000, 189000, 173000, 192000, 147000, 185000, 187000, 195000, 190000, 178000, 112000, 146000, 125000, 177000, 160000, 159000, 140000, 118000, 104000, 195000, 154000, 174000, 125000, 176000, 102000, 129000]
                var newArr = newArr4.concat(newArr1,newArr2,newArr3)
                this.dataList.forEach((item, index) => {
                    item.goods_pin = arr[index];
                    item.goods_comments = newArr[index];
                    item.goods_img_arr = goods_img_arr;
                })
            }
            // 渲染页面
            render() {

                var str = '';
                this.dataList.forEach(item => {
                    str += `
                    <li data-id="${item.goods_id}">
                    <div class="img">
                        <img src="${item.img_small_logo}" alt="" data-id="${item.goods_id}">
                    </div>
                    <div class="price">
                        ￥<span data-id="${item.goods_id}">${item.current_price}</span>
                    </div>
                    <div class="desc">
                        <p class="desc_one" data-id="${item.goods_id}">${item.title}</p>
                        <p class="desc_two" data-id="${item.goods_id}">${item.category}</p>
                    </div>
                    <div class="product_news">
                        <div class="left">
                            <span>月成交</span>
                            <span class="product_m_num" data-id="${item.goods_id}">${item.goods_pin}</span>
                        </div>
                        <div class="comments">
                            <span>评价</span>
                            <span class="product_comments" data-id="${item.goods_id}">${item.goods_comments}</span>
                        </div>
                        <div class="service">
                        </div>
                    </div>
                </li>`
                })
                $('#main .goods ul').html(str)
                // $('.paging_list').html(str)
                $(".paging_list").paging({
                    PageNum:40, //每页显示数目
                    pageMax:true, //按钮长度是否显示
                    pageMaxHideShow:false, //在最后一个的时候是否隐藏按钮长度
                    pageDownUpHide:false, //到第一个或最后一个是否让上一页或下一页消失
                    // pageInput:true, //是否使用文本框输入跳转
                    pagingBtnHide:false, //是否让按钮变为一个
                    pagingBtnPaging:true,//按钮是否分页
                    pagingDisplay:"block", //显示的属性，弹性盒子还是块化
                })
                
            }
            // 人气
            sentiment() {
                $('#main .goods_top .comprehensive span').eq(1).click(() => {
                    for (let i = 0; i < this.dataList.length - 1; i++) {
                        for (let j = 0; j < this.dataList.length - 1 - i; j++) {
                            if (this.dataList[j].goods_pin < this.dataList[j + 1].goods_pin) {
                                let temp = this.dataList[j];
                                this.dataList[j] = this.dataList[j + 1];
                                this.dataList[j + 1] = temp
                            }
                        }
                    }
                    this.render();
                })
            }
            // 价格
            price(){
                let flag = true
                $('#main .goods_top .comprehensive span').eq(4).click(() => {                   
                    if(flag){
                        for (let i = 0; i < this.dataList.length - 1; i++) {
                            for (let j = 0; j < this.dataList.length - 1 - i; j++) {
                                if (this.dataList[j].current_price*1 < this.dataList[j + 1].current_price*1) {
                                    let temp = this.dataList[j];
                                    this.dataList[j] = this.dataList[j + 1];
                                    this.dataList[j + 1] = temp
                                }
                            }
                        }
                        flag = !flag;
                    }else{
                        for (let i = 0; i < this.dataList.length - 1; i++) {
                            for (let j = 0; j < this.dataList.length - 1 - i; j++) {
                                if (this.dataList[j].current_price*1 > this.dataList[j + 1].current_price*1) {
                                    let temp = this.dataList[j];
                                    this.dataList[j] = this.dataList[j + 1];
                                    this.dataList[j + 1] = temp
                                }
                            }
                        }
                        flag = !flag;
                    }
                    this.render();
                })
            }
            // 分类
            category(){
                this.species.onclick = (e)=>{
                    var e = e || window.event;
                    var target = e.target || e.srcElement;
                    this.dataList = res.slice(0, 400);
                    if(target.className == 'one'){
                        this.dataList = this.dataList.filter(item =>{return item.category == '童装玩具'})
                        this.render();
                        this.price();
                        this.sentiment();
                    }
                    
                    if(target.className == 'nei'){
                        this.dataList = this.dataList.filter(item =>{return item.category == '内衣配饰'})
                        this.render();
                        this.price();
                        this.sentiment();
                    }
                    if(target.className == 'big'){
                        this.dataList = this.dataList.filter(item =>{return item.category == '大家电'})
                        this.render();
                        this.price();
                        this.sentiment();
                    }
                    if(target.className == 'out'){
                        this.dataList = this.dataList.filter(item =>{return item.category == '海外购'})
                        this.render();
                        this.price();
                        this.sentiment();
                    }
                    if(target.className == 'some'){
                        this.dataList = this.dataList.filter(item =>{return item.category == '其他'})
                        this.render();
                        this.price();
                        this.sentiment();
                    }
                    if(target.className == 'jewelry'){
                        this.dataList = this.dataList.filter(item =>{return item.category == '珠宝首饰'})
                        this.render();
                        this.price();
                        this.sentiment();
                    }
                    if(target.className == 'smart'){
                        this.dataList = this.dataList.filter(item =>{return item.category == '智能设备'})
                        this.render();
                        this.price();
                        this.sentiment();
                    }
                    if(target.className == 'leather'){
                        this.dataList = this.dataList.filter(item =>{return item.category == '皮具箱包'})
                        this.render();
                        this.price();
                        this.sentiment();
                    }
                }
            }
            // 品牌
            the_brand(){
                this.ul_p.onclick = (e)=>{
                    var e = e || window.event;
                    this.dataList = res.slice(0,400)
                    var target = e.target || e.srcElement;
                    if(target.className == 'south'){
                        this.dataList = this.dataList.filter(item=>{return item.title.includes('南极人')})
                        this.render();
                        this.price();
                        this.sentiment();
                    }
                    if(target.className == 'mi'){
                        this.dataList = this.dataList.filter(item=>{return item.title.includes('小米')})
                        this.render();
                        this.price();
                        this.sentiment();
                    }
                }
            }
            // 搜索功能
            search(){
                

                    //给serch_btn绑定点击事件
                    this.btn.onclick = () => {
                        this.dataList = res.slice(0,400)
                        this.dataList = this.dataList.filter(item => {
                            return item.title.includes(this.input.value)
                        })
                        this.render()
                    }
                    // //给输入框绑定键盘事件 满足条件执行和btn一样的事件
                    // this.input.onkeyup = (e) => {
                    //     var e = e || window.event
                    //     var code = e.keyCode || e.which
                    //     if (code == 13) {
                    //         this.dataList = this.copyData.filter(item => {
                    //             return item.goods_name.includes(this.input.value)
                    //         })
                    //         console.log(this.dataList)
                    //         this.render()
                    //     }
                    // }
                
            }
            // 点击li跳转到详情页面
            clicktoDetails(){
                this.ul.addEventListener('click',(e)=>{
                    var e = e || window.event;
                    var target = e.target || e.srcElement;
                    if(target.nodeName =='IMG'){
                        let targetId = target.dataset.id;
                        let goods = this.dataList.find(item=>{
                            return item.goods_id == targetId;
                        })
                        let list = [];
                        list.push(goods);
                        console.log(list);
                        localStorage.setItem('details',JSON.stringify(list))
                        location.href = 'http://localhost:2206/details.html'
                    }
                    if(target.nodeName =='IMG'){
                        let targetId = target.dataset.id;
                        let goods = this.dataList.find(item=>{
                            return item.goods_id == targetId;
                        })
                        let list = [];
                        list.push(goods);
                        console.log(list);
                        localStorage.setItem('details',JSON.stringify(list))
                        location.href = 'http://localhost:2206/details.html'
                    }
                })
            }
        }
        new Mr_miao
    })





})

